package br.unb.statistic;

/**
 * ClasseService : defines the class that owns the packet (diffservices)
 *
 * @author maria ladoue
 * @version 0.1, 31/07/02 
 */
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.DatagramSocketImpl;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketOptions;

import br.unb.utils.Console;
import br.unb.utils.Resource;

public class ClasseService extends DatagramSocketImpl implements SocketOptions {

    DatagramSocket ds;
    public static int IP_TOS = 0; // not yet implemented

    public ClasseService() throws SocketException {
        ds = new DatagramSocket();
        setOption(IP_TOS, new Integer(0));
    }

    public ClasseService(DatagramSocket unDS, int uneValeurCoS) throws SocketException {
        ds = unDS;
        setOption(IP_TOS, new Integer(uneValeurCoS));
    }

    // Definition of some methods of class
    // To get the value as an integer
    public int getClasseService() throws SocketException {
        return ((Integer) this.getOption(0)).intValue();
    }

    public void setDS(DatagramSocket unDS) {
        ds = unDS;
    }

    /**
     * Returns the socket through which it sends datagrams
     * @return the socket
     */
    public DatagramSocket getDS() {
        return ds;
    }

    // methods inherited from interface SocketOptions
    public void setOption(int optID, Object value) throws SocketException {
        Object o = value;
        if (o instanceof Integer) {
            if (optID == IP_TOS) {
                ds.setTrafficClass(((Integer) o).intValue());
            } else {
                Console.printError(
                        Resource.getString("THIS_PARAMETER_COUDN'T_BE_RETRIEVE"));
            }
        } else {
            Console.printError(
                    Resource.getString("VALUE_MUST_BE_AN_INT"));
        }
    }

    public Object getOption(int optID) throws SocketException {
        int i;
        Object o;
        if (optID == IP_TOS) {
            try {
                i = ds.getTrafficClass();
                o = new Integer(i);
                return o;
            } catch (SocketException e) {
                Console.printError(
                        Resource.getString("THERE_IS_AN_ERROR_OBTAINING_THE_TRAFFIC_CLASS_OR_TYPE-OF-SERVICE_VALUE"));
                return new Boolean("false");
            }
        } else {
            o = new Boolean("false");
            return o;
        }
    }

    // Methods inherited from class DatagramSocketImpl
    protected void bind(int lport, InetAddress laddr) {
    }

    protected void close() {
    }

    protected void create() {
    }

    protected int getTimeToLive() {
        return 0;
    }

    protected byte getTTL() {
        return 0;
    }

    protected void join(InetAddress inetaddr) {
    }

    protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) {
    }

    protected void leave(InetAddress inetaddr) {
    }

    protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) {
    }

    protected int peek(InetAddress i) {
        return 0;
    }

    protected int peekData(DatagramPacket p) {
        return 0;
    }

    protected void receive(DatagramPacket p) {
    }

    protected void send(DatagramPacket p) {
    }

    protected void setTimeToLive(int ttl) {
    }

    protected void setTTL(byte ttl) {
    }
    /**
     * Method: Main for testing the class
     */
    /*
    public static void main(String[] args) throws SocketException {

    ClasseService uneCS = new ClasseService();
    System.out.println(uneCS.getOption(0).toString());
    uneCS.setOption(0, new Integer(2));
    System.out.println(uneCS.getOption(0).toString());


    }// fin du main
     */
} 

